package com.xigua.xcar.widgets.ShopList;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xigua.xcar.R;
import com.xigua.xcar.data.ShopListData;

public class ShopListAdapter extends BaseAdapter {
	private static final String tag = "ShopListAdapter";
	private ArrayList<ShopListData> mArray = null;
	private LayoutInflater mInflater = null;

	public ShopListAdapter(Context context) {
		Log.i(tag, "ShopListAdapter");
		if (null != context) {
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
	}

	public void setArray(ArrayList<ShopListData> array) {
		mArray = array;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (null != mArray) {
			return mArray.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int arg0) {
		return mArray.get(arg0 % (mArray.size()));
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	private void updateView(ShopListData data, View convertView, int index) {
		ImageView shopPhoto = (ImageView) convertView
				.findViewById(R.id.image_shop_shopPhoto);
		TextView shopName = (TextView) convertView
				.findViewById(R.id.txt_shop_shopName);
		TextView saleInfo = (TextView) convertView
				.findViewById(R.id.txt_shop_saleInfo);
		TextView distance = (TextView) convertView
				.findViewById(R.id.txt_shop_distance);
		if (null == data.getImageUrl() || "".equals(data.getImageUrl())) {
			shopPhoto.setBackgroundResource(R.drawable.default_shopback);
		} else {
			// todo: get Image from net
		}
		if (null != data.getShopName()) {
			shopName.setText(data.getShopName());
		}
		if (null != data.getSaleInfo()) {
			saleInfo.setText(data.getSaleInfo());
		}
		if (null != data.getDistance()) {
			if(data.getDistance().equals("")){
				distance.setText("");
			}else{
				int realDistance = Integer.parseInt(data.getDistance());
				String strDistance = null;
				if(realDistance>=1000){
					strDistance = (realDistance/1000) + "." + ((realDistance%1000)/100) + "千米";
				}else{
					strDistance = realDistance + "米";
				}
				distance.setText(strDistance);
			}
		}
		convertView.setTag(data);
//		if(0 == index){
//			convertView.setBackgroundResource(R.drawable.list_back_top);
//			RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams)shopPhoto.getLayoutParams();
//			param.setMargins(15, 0, 0, 0);
//			shopPhoto.setLayoutParams(param);
//			param = (RelativeLayout.LayoutParams)distance.getLayoutParams();
//			param.setMargins(0, 0, 15, 0);
//			distance.setLayoutParams(param);
//		}else if((mArray.size()-1) == index){
//			convertView.setBackgroundResource(R.drawable.list_back_bottom);
//		}else{
//			convertView.setBackgroundResource(R.drawable.list_back_middle);
//			RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams)shopPhoto.getLayoutParams();
//			param.setMargins(22, 0, 0, 0);
//			shopPhoto.setLayoutParams(param);
//			param = (RelativeLayout.LayoutParams)distance.getLayoutParams();
//			param.setMargins(0, 0, 22, 0);
//			distance.setLayoutParams(param);
//		}
		convertView.setBackgroundResource(R.drawable.list_back_middle);
	}

	@Override
	public View getView(int index, View convertView, ViewGroup arg2) {
		index = (index % (mArray.size()));
		ShopListData data = mArray.get(index);
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.shoplist_item, null);
		}
		updateView(data, convertView,index);
		return convertView;
	}

}
